package com.example.demo;

import java.util.List;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AsyncServiceImpl implements AsyncService {

	@PersistenceContext
	EntityManager em;

	@Async
	@Transactional
	public void task1() {

		em.persist(User.builder().name(UUID.randomUUID().toString()).build());

		System.out.println(Thread.currentThread().getName());

		Query query = em.createNativeQuery("select * from user").unwrap(SQLQuery.class)
				.addScalar("id", StandardBasicTypes.LONG).addScalar("name", StandardBasicTypes.STRING)
				.setResultTransformer(Transformers.aliasToBean(User.class));

		List<?> list = query.list();

		System.out.println(list);
		
		System.out.println("end...");

	}

}
